const express = require("express")
const router = express.Router()
const {addChat,delChat,searchChat,searchLastChat} = require("../control/chat")
const {SuccessModel,ErrorModel} = require("../model/resModel")
const passport = require("passport")

router.get("/add",passport.authenticate("jwt",{session:false}),async (req,res)=>{
  let data =await addChat(req,res)
  console.log(data);
  if(data&&data.affectedRows>0){
    res.json(new SuccessModel("添加成功"))
  }else{
    res.json(new ErrorModel("添加失败"))
  }
  
})
router.post("/del",passport.authenticate("jwt",{session:false}),async (req,res)=>{
  let data =await delChat(req,res)
  console.log(data);
  if(data&&data.affectedRows>0){
    res.json(new SuccessModel("删除成功"))
  }else{
    res.json(new ErrorModel("删除失败"))
  }
})
router.get("/search",async (req,res)=>{
  let data =await searchChat(req,res)
  if(data&&data.length>0){
    res.json(new SuccessModel(data,"查找成功"))
  }else{
    res.json(new ErrorModel("查找失败"))
  }
})
router.get("/lastSearch",async (req,res)=>{
  let data =await searchLastChat(req,res)
  if(data&&data.length>0){
    res.json(new SuccessModel(data,"查找成功"))
  }else{
    res.json(new ErrorModel("查找失败"))
  }
})



module.exports=router